package com.elai.web.controller.baseconfig;

import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.core.page.TableDataInfo;
import com.elai.common.enums.BusinessType;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.baseconfig.domain.EventItemPropertyDO;
import com.elai.baseconfig.service.EventItemPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 事件项属性Controller
 *
 * @author elai
 * @date 2021-04-03
 */
@RestController
@RequestMapping("/config/property")
public class EventItemPropertyController extends BaseController
{
    @Autowired
    private EventItemPropertyService eventItemPropertyService;

    /**
     * 查询事件项属性列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EventItemPropertyDO eventItemProperty)
    {
        startPage();
        List<EventItemPropertyDO> list = eventItemPropertyService.selectEventItemPropertyList(eventItemProperty);
        return getDataTable(list);
    }

    @GetMapping("/getPropertyListByType")
    public AjaxResult getPropertyListByType(EventItemPropertyDO eventItemProperty)
    {
        List<EventItemPropertyDO> list = eventItemPropertyService.selectEventItemPropertyList(eventItemProperty);
        return AjaxResult.success(list);
    }

    @GetMapping("/getAllItemPropertyMap")
    public AjaxResult getAllItemPropertyMap()
    {
        List<EventItemPropertyDO> list = eventItemPropertyService.selectEventItemPropertyList(new EventItemPropertyDO());
        Map<String, List<EventItemPropertyDO>> propertyMap = list.stream().collect(Collectors.groupingBy(EventItemPropertyDO::getValueType));
        return AjaxResult.success(propertyMap);
    }

    /**
     * 导出事件项属性列表
     */
    @PreAuthorize("@ss.hasPermi('config:property:export')")
    @Log(title = "事件项属性", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EventItemPropertyDO eventItemProperty)
    {
        List<EventItemPropertyDO> list = eventItemPropertyService.selectEventItemPropertyList(eventItemProperty);
        ExcelUtil<EventItemPropertyDO> util = new ExcelUtil<EventItemPropertyDO>(EventItemPropertyDO.class);
        return util.exportExcel(list, "property");
    }

    /**
     * 获取事件项属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('config:property:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(eventItemPropertyService.selectEventItemPropertyById(id));
    }

    /**
     * 新增事件项属性
     */
    @PreAuthorize("@ss.hasPermi('config:property:add')")
    @Log(title = "事件项属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EventItemPropertyDO eventItemProperty)
    {
        return toAjax(eventItemPropertyService.insertEventItemProperty(eventItemProperty));
    }

    /**
     * 修改事件项属性
     */
    @PreAuthorize("@ss.hasPermi('config:property:edit')")
    @Log(title = "事件项属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EventItemPropertyDO eventItemProperty)
    {
        return toAjax(eventItemPropertyService.updateEventItemProperty(eventItemProperty));
    }

    /**
     * 删除事件项属性
     */
    @PreAuthorize("@ss.hasPermi('config:property:remove')")
    @Log(title = "事件项属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eventItemPropertyService.deleteEventItemPropertyByIds(ids));
    }
}
